<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            position: 0;
            list-style: none;
        }
        #pg{
           width: 500px;
            margin: 0 auto;
        }
        .box{
            text-align:center;
        }
        #int{
            width: 200px;
        }
    </style>
</head>
<body>
    <div id="pg">
       <div class="box">
            <!-- 输入框 -->
        <h1>to do list</h1>
        <input type="text" v-model='shop' placeholder="请输入事件" id="int" @keyup.enter='add'>
       </div>
        <!-- 列表 -->
        <com :bb='top' @sub='back' @sum='sumall'>
           <template #title='vv'>
               <h3>{{ vv.$title.title1}}</h3>
           </template>
        </com>
        
        <com :bb="over"  @sub='back' @sum='sumall'>
            <template #title='vv'>
                <h3>{{ vv.$title.title2}}</h3>
            </template>
        </com>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        let com={
            props: ['bb'],
            template:`<div>
                <slot name='title' :$title="titleall"></slot>
            <ul>
               <li v-for='(item,index) in bb' :key="item.id">
                <input type="checkbox"  v-model="item.flag" @change="huan(item)"/>
                <span>{{ item.text }}</span>
                <button @click='del(item)' >删除</button>
               </li>
            </ul>
            </div>`,
            data () {
                return {
                        titleall:{
                            title1:'进行时',
                            title2:'完成时'
                        }
                }
            },
            methods: {
                // 选中
               huan(item){
                    this.$emit('sum',item)
                },
                // 删除
                del(item){
                    this.$emit('sub',item)
                }
            }
        }
        var app = new Vue({
            el:'#pg',
            data:{
                top:[],
                shop:'',
                over:[],
                id:1
            },
            methods:{
                add:function(sondata){
                   if(!this.shop){
                       alert('请输入内容')
                       return
                   }else{
                    this.top.push({
                        id:this.id++,
                        text:this.shop,
                        flag:false
                    });
                    this.shop=""
                   }
                },
                back(res){
                    if(res.flag){
                        this.over.splice(this.over.indexOf(res),1)
                    }else{
                        this.top.splice(this.top.indexOf(res),1)
                    }
                },
                // 更改数据
                sumall(res){
                    if(res.flag){
                        this.over.push(res)
                        this.top.splice(this.top.indexOf(res),1)
                    }else{
                        this.top.push(res)
                        this.over.splice(this.over.indexOf(res),1)
                    }
                }
            },
            components: {
                com,
            }
        })
    </script>
</body>
</html>